/**
 * Created by Weizehua on 2017/1/26.
 */

import {Injectable} from "@angular/core";

export interface ImageInfo {
    size:number,
    format: string,
    width: number,
    height: number,
}


@Injectable()
export class PostCardRequest {
    constructor(){}

    async getSourceInfo(ossurl:string):Promise<ImageInfo>{
        return new Promise<any>((resolve, reject) => {
            // create request
            let xhr = new XMLHttpRequest();
            xhr.open('GET', ossurl + '?x-oss-process=image/info', true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        let res = JSON.parse(xhr.responseText);
                        console.log(res);
                        resolve({
                            format: res.Format.value,
                            size: res.FileSize.value,
                            width: res.ImageWidth.value,
                            height: res.ImageHeight.value,
                        });
                    }
                    else {
                        reject(`Upload finished with error status: (${xhr.status}) ${xhr.statusText}`);
                    }
                }
            };
            xhr.onabort = ev => reject(ev);
            xhr.onerror = ev => reject(ev);
        })
    }
}
